﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyBanVeMayBay.Resources
{
    class xulydulieu
    {
     
        //---------------------------------------------------
  
        public SqlConnection taoketnoi()
        {
            return new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=My_Data;Integrated Security=True");
        }

        //----------------------------------------------------------
        public DataTable taobang(string sql)
        {
            SqlConnection myconnect = taoketnoi();
            SqlDataAdapter adt = new SqlDataAdapter(sql, myconnect);
            DataTable dtb = new DataTable();
            adt.Fill(dtb);
            myconnect.Close();
            return dtb;
        }

        //----------------------------------------------------------------
        public void nonquery(string sql)
        {
            SqlConnection myconnect = taoketnoi();
            SqlCommand mycommand = new SqlCommand(sql, myconnect);
            myconnect.Open();
            mycommand.ExecuteNonQuery();
            myconnect.Close();
            mycommand.Dispose();
        }
        //--------------------------------------------------------------
        public DataSet dtset(string sql)
        {
            SqlConnection myconnect = taoketnoi();
            myconnect.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(sql,myconnect);
            DataSet dts = new DataSet();
            adapter.Fill(dts);
            myconnect.Close();
            return dts;
        }
        //-------------------------------------------------------------------
        public SqlDataReader Reader(string sql)
        {
            SqlConnection conn = taoketnoi();
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlDataReader Reader = comm.ExecuteReader();
            comm.Dispose();
            return Reader;
        }
        //------------------------------------------------------------------------
        public DataTable AutoNumberedTable(DataTable SourceTable)
        { 
            DataTable ResultTable = new DataTable(); 
            DataColumn AutoNumberColumn = new DataColumn(); 
            AutoNumberColumn.ColumnName="STT";
            AutoNumberColumn.DataType = typeof(int); 
            AutoNumberColumn.AutoIncrement = true; 
            AutoNumberColumn.AutoIncrementSeed = 1; 
            AutoNumberColumn.AutoIncrementStep = 1; 
            ResultTable.Columns.Add(AutoNumberColumn); 
            ResultTable.Merge(SourceTable); 
            return ResultTable;
        }
    }
}
